package com.peanut.baby.mvp.calendar;

import com.peanut.baby.model.CalendarRecord;
import com.peanut.devlibrary.util.StringUtil;

import java.util.Date;
import java.util.List;

/**
 * Created by anonymouslystudio on 2017/9/7.
 */

public class MenseUtil {

    /***
     * 获取距离date最近的月经状态记录
     * @param referDate 对比日期
     * @param source    选择集
     * @param comOrGo   来了还是走了
     * @return
     */
    public static CalendarRecord getRecentMenseRecord(Date referDate, List<CalendarRecord> source,String comOrGo){
        if(referDate == null || source == null || source.isEmpty()){
            return null;
        }
        CalendarRecord recent = null;
        int diffDay = 0;

        int sourceSize = source.size();
        for(int i=0;i<sourceSize;i++){
            CalendarRecord record = source.get(i);

            if(StringUtil.isNullOrEmpty(record.dayima) || !record.dayima.equals(comOrGo)){
                continue;
            }
            int daysBetween = CalUtil.daysBetween(CalUtil.dateToString(referDate),record.date);
            if(diffDay == 0){
                diffDay = Math.abs(daysBetween);
                recent = record;
            }else{
                if(Math.abs(daysBetween) < diffDay){
                    diffDay = Math.abs(daysBetween);
                    recent = record;
                }
            }


        }
        return recent;
    }
}
